define([
  'EventBus',
], function (EventBus) {
  'use strict';
  function calcKey(key) {
    return `__workflow__${key}`
  }
  const methods = {
    getBaseInfo: calcKey('getBaseInfo'),
    initEventBus: calcKey('initEventBus'),
    getInitData: calcKey('getInitData'),
    beforeSubmit: calcKey('beforeSubmit'),
    processGetValue: calcKey('processGetValue'),
    processSetValue: calcKey('processSetValue'),
  }
  return {
    data() {
      return {
        baseInfoRef: 'baseInfo',
        businessObjectRef: 'businessObject',
        currentNodeProcessInfo: null,
        activityId: '', //节点id
        nextActivityId: '', //下一个节点
        businessKey: '',
      }
    },
    created() {
      this[methods.initEventBus]()
    },
    methods: {
      async [methods.getBaseInfo]() {
        let baseInfo = {}
        // 从流程按钮点进来 获取不到提交信息
        if (this.$refs[this.baseInfoRef]) {
          baseInfo = await this.$refs[this.baseInfoRef].getFormData()  //获取基本信息
        }
        return baseInfo
      },
      // 注册事件
      [methods.initEventBus]() {
        EventBus.once('process:customForm:setValue', this[methods.processSetValue])
        EventBus.once('process:customForm:getValue', this[methods.processGetValue])
        EventBus.once('workflow:machining:before:submit', this[methods.beforeSubmit])
      },
      // 获取初始化信息
      async [methods.getInitData](data) {
        let reviewItemList = []
        if (this.$refs[this.businessObject]) {
          reviewItemList = this.$refs[this.businessObject].getTableData().map(c => ({ oid: c.id }))
        }
        // 获取流程下一个节点
        let sessionId = ""
        let reqData = {
          processInstanceId: this.currentNodeProcessInfo.processInstanceId,
          routeFlag: data.fp_route_flag,
          activityId: this.activityId
        }
        const resp = await $.elApi.workflowCom.fetchProcessNextTask(reqData)
        if (resp.code === "200") {
          sessionId = resp.res.data.length > 0 ? resp.res.data[0].nextKey : ""
        }
        let baseInfo = await this[methods.getBaseInfo]()
        const formJsonStr = {
          reviewItemList
        }
        const _userMap = {}
        const _roleMap = {}
        Object.keys(data).forEach(c => {
          if (c.indexOf("_assignee") !== -1 && c.length > 9) {
            _userMap[c] = data[c]
          }
          if (c.indexOf("_role") !== -1 && c.length > 5) {
            _roleMap[c] = data[c]

          }
        })
        const parmar = {
          "baseForm": {
            "processBasicInfo": baseInfo,
            "businessForm": { reviewItemList },
            "businessFormJsonStr": JSON.stringify(formJsonStr),
          },
          "baseSubmitTaskDto": {
            'routeFlag': data.fp_route_flag,
            "sessionId": sessionId,
            "taskId": this.currentNodeProcessInfo.taskId,
            "comment": data.comment,
            "dueDate": data.dueDate,
            "priority": data.priority,
            "userMap": _userMap,
            "roleMap": _roleMap,
            "uploadFileIds": data.uploadFileIds

          },
          "pboId": this.currentNodeProcessInfo.businessKey,
          "processInstanceId": this.currentNodeProcessInfo.processInstanceId,
        }
        return parmar
      },
      // 提交之前
      async [methods.beforeSubmit](evt, data) {
        if (data?.baseForm) {
          const baseInfo = await this[methods.getBaseInfo]()
          data.baseForm.processBasicInfo = Object.assign(data.baseForm.processBasicInfo, baseInfo)
        } else {
          data = await this[methods.getInitData](data)
        }
        this.submit(data)
      },
      [methods.processGetValue](evt, callback) {
        callback({})
      },
      async [methods.processSetValue](evt, data) {
        this.currentNodeProcessInfo = data
        this.activityId = data.activityId
        this.getdata(data)
      },
      // 获取数据
      handleCloseClick() {
        $.form.backSidepage()
      },
      getdata(data) {
        console.log(data)
      },
      submit(data) {
        console.log(data)
      }
    }
  }
});